package com.boost.socialNetwork.client;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.CellWidget;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.reveregroup.gwt.facebook4gwt.Facebook;
import com.reveregroup.gwt.facebook4gwt.FacebookStory;
import com.reveregroup.gwt.facebook4gwt.user.FacebookUser;
import com.reveregroup.gwt.facebook4gwt.user.UserField;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Home extends LayoutPanel { 
	
	private Label head;
	private String userKey;
	
	private final UserServiceAsync greetService = GWT
	.create(UserService.class);
	private FlexTable myIdeas;
	private FlexTable others;
	private IdeaPage idea;
	private FlexTable fbTable;
	
	public Home() {
		super();
		greetService.getConnected(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error");

			}

			@Override
			public void onSuccess(String result) {
				if (result == null)
					HistoryManager.newHistory("PageLogin");

			}
		});		
		
		System.out.println("Da5al el Home");

		head = new Label("");						
		Label lblMyIdeas = new Label("My Ideas");
		lblMyIdeas.setStyleName("gwt-title");
		add(lblMyIdeas);
		setWidgetLeftWidth(lblMyIdeas, 338.0, Unit.PX, 80.0, Unit.PX);
		setWidgetTopHeight(lblMyIdeas, 79.0, Unit.PX, 21.0, Unit.PX);
		
		Label lblOtherIdeas = new Label("Other Ideas");
		lblOtherIdeas.setStyleName("gwt-title");
		add(lblOtherIdeas);
		setWidgetLeftWidth(lblOtherIdeas, 495.0, Unit.PX, 88.0, Unit.PX);
		setWidgetTopHeight(lblOtherIdeas, 79.0, Unit.PX, 21.0, Unit.PX);
		
		myIdeas = new FlexTable();
		add(myIdeas);
		setWidgetLeftWidth(myIdeas, 338.0, Unit.PX, 100.0, Unit.PX);
		setWidgetTopHeight(myIdeas, 106.0, Unit.PX, 100.0, Unit.PX);
		
		others = new FlexTable();
		add(others);
		setWidgetLeftWidth(others, 483.0, Unit.PX, 100.0, Unit.PX);
		setWidgetTopHeight(others, 106.0, Unit.PX, 100.0, Unit.PX);
		
		fbTable = new FlexTable();
		add(fbTable);
		setWidgetLeftWidth(fbTable, 116.0, Unit.PX, 100.0, Unit.PX);
		setWidgetTopHeight(fbTable, 106.0, Unit.PX, 100.0, Unit.PX);
		
		Image image = new Image("socialnetwork/facebook.png");
		fbTable.setWidget(0, 0, image);
		image.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				System.out.println("clicked");
				Facebook.login(new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@SuppressWarnings("unchecked")
					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							Facebook.APIClient().users_getLoggedInUser(new AsyncCallback() {
								public void onSuccess(FacebookUser result) {
									//userName.setText(result.getName());
									//userImage.setUrl(result.getPic());
									//userInfoPanel.setVisible(true);
								  }
								    public void onFailure(Throwable caught) {
								    	Window.alert("A fatal facebook exception");
								  }
									@Override
									public void onSuccess(Object result) {
										FacebookUser fb = (FacebookUser) result;
										Label username = new Label(fb.getName());
										Label birth = new Label(fb.getBirthday());
										String url = fb.getPic();
										fbTable.clear();
										fbTable.setWidget(0, 0, username);
										fbTable.setWidget(1, 0, birth);
										Image pic = new Image();
										pic.setUrl(url);
										fbTable.setWidget(2, 0, pic);
									}
								}, UserField.NAME, UserField.STATUS, UserField.PIC, UserField.BIRTHDAY);
							
						}
						
					}
					
				});
			}
		});
				
		greetService.getConnected(new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				userKey = result;
				getIdeas();}
				
				
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});						
/*HistoryManager.addValueChangeHandler(new ValueChangeHandler<String>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
			HistoryManager.HistoryChange(event);
				
			}
		});*/
	}	
public void getIdeas() {

		greetService.retrieveIdeas(userKey,
				new AsyncCallback<List<String>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(final List<String> result) {
						if (result.size() == 0) {
							Label l = new Label("You have no Ideas");
							myIdeas.setWidget(0, 0, l);
						} else {
							for (int i = 0; i < result.size(); i++) {
								String[] a = result.get(i).split(",");
								Label l = new Label(a[3]);
								l.setStyleName("gwt-LabelLink");
								myIdeas.setWidget(i, 0, l);
								Label l2 = new Label();
								l.addClickHandler(new ClickHandler() {

									@Override
									public void onClick(ClickEvent event) {
										for (int i = 0; i < result.size(); i++) {
											if (myIdeas.getWidget(i, 0).equals(
													event.getSource())) {
												
												HistoryManager.setIdea(result
														.get(i));
												HistoryManager
														.setCanDelete(true);
												HistoryManager
														.newHistory("PageIdea");
												// idea = new
												// IdeaPage(result.get(i),
												// true);
												// RootPanel.get("post").clear();
												// RootPanel.get("head").clear();
												// RootPanel.get("post").add(idea);
												break;
											}
										}

									}
								});
							}
						}
					}
				});

		greetService.retrieveOtherIdeas(userKey,
				new AsyncCallback<List<String>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(final List<String> result) {
						if (result.size() == 0) {
							Label l = new Label("There are no other Ideas");
							others.setWidget(0, 0, l);
						} else {
							for (int i = 0; i < result.size(); i++) {
								String[] a = result.get(i).split(",");
								Label l = new Label(a[3]);
								l.setStyleName("gwt-LabelLink");
								others.setWidget(i, 0, l);
								l.addClickHandler(new ClickHandler() {

									@Override
									public void onClick(ClickEvent event) {
										for (int i = 0; i < result.size(); i++) {
											if (others.getWidget(i, 0).equals(
													event.getSource())) {
												HistoryManager.setIdea(result
														.get(i));
												HistoryManager
														.setCanDelete(false);
												HistoryManager
														.newHistory("PageIdea");
												// IdeaPage idea = new
												// IdeaPage(result.get(i),
												// false);
												// RootPanel.get("post").clear();
												// RootPanel.get("head").clear();
												// RootPanel.get("post").add(idea);
												break;
											}
										}

									}
								});
							}
						}

					}
				});
	}	
	
	
	public Label getHead() {
		return head;
	}

	public void setHead(String text) {
		head.setText("Welcome " + text);
	}
	public FlexTable getMyIdeas() {
		return myIdeas;
	}

	public void setMyIdeas(FlexTable myIdeas) {
		this.myIdeas = myIdeas;
	}

	public FlexTable getOthers() {
		return others;
	}

	public void setOthers(FlexTable others) {
		this.others = others;
	}
}
